//
//  CoinSelectView.m
//  CEX
//
//  Created by xiebin on 2018/1/19.
//  Copyright © 2018年 mingthink. All rights reserved.
//

#import "CoinSelectView.h"
#define kSelectViewH 160
@implementation CoinSelectView
{
    UIView *shadowV;
    UIView *view_indicateLine;
    NSMutableArray *marr_btn;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self= [super initWithFrame:frame];
    if (self) {
        marr_btn=[[NSMutableArray alloc]init];
        [self initSubViews];
    }
    return self;
}

-(void)initSubViews{
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeSelectView)];
    tap.delegate=self;
    tap.numberOfTapsRequired=1;
    [self addGestureRecognizer:tap];
    
    _selectView=[[UIView alloc]initWithFrame:CGRectMake(0, -kSelectViewH, DEVW, kSelectViewH)];
    _selectView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_selectView];
    _selectView.sd_layout.topSpaceToView(self,-kSelectViewH).leftSpaceToView(self,0).rightSpaceToView(self,0).heightIs(kSelectViewH);
    
    CGFloat switchBG_height=40;
    UIView *view_switch = [[UIView alloc]init];
    [_selectView addSubview:view_switch];
    view_switch.backgroundColor = GET_HEX_COLOR(TABLE_SEPARATOR_CONTENT);
    view_switch.sd_layout.topSpaceToView(_selectView,0).leftSpaceToView(_selectView,0).rightSpaceToView(_selectView,0).heightIs(switchBG_height);
    
    NSArray *itemArr=@[@"兑USDT",@"兑BTC",@"兑ETH",@"兑QTUM"];
    CGFloat btnWidth=DEVW/itemArr.count;
     CGFloat btnHeight=view_switch.height-1;
    for (int i=0; i<itemArr.count; i++) {
        UIButton *btn_switch = [UIButton buttonWithType:UIButtonTypeCustom];
        [view_switch addSubview:btn_switch];
        btn_switch.tag=100+i;
        [btn_switch setTitle:itemArr[i] forState:UIControlStateNormal];
        [btn_switch setTitleColor:GET_HEX_COLOR(TEXT_DEEP_BLACK) forState:UIControlStateNormal];
        if (i==0) {
                btn_switch.backgroundColor = GET_HEX_COLOR(TABLE_SEPARATOR_GRAY);
        }else{
                    btn_switch.backgroundColor = [UIColor clearColor];
        }
        btn_switch.titleLabel.font = GET_SYSTEM_FONT(TEXT_FONT_30);
        [btn_switch addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventTouchUpInside];
        btn_switch.sd_layout.topSpaceToView(view_switch,0).leftSpaceToView(view_switch,btnWidth*i).widthIs(btnWidth).heightIs(btnHeight);
        [marr_btn addObject:btn_switch];

    }
    view_indicateLine = [[UIView alloc]init];
    [view_switch addSubview:view_indicateLine];
    view_indicateLine.backgroundColor = GET_HEX_COLOR(CELL_LABLE_BORDER_RED);
    view_indicateLine.sd_layout.bottomSpaceToView(view_switch,0).leftSpaceToView(view_switch,0).widthIs(btnWidth).heightIs(1);
}
-(void)switchClick:(UIButton *)btn{
    for (UIButton *tempBtn in marr_btn) {
        if (btn==tempBtn) {
                 tempBtn.backgroundColor = GET_HEX_COLOR(TABLE_SEPARATOR_GRAY);
        }else{
              tempBtn.backgroundColor = [UIColor clearColor];
        }
    }
    view_indicateLine.sd_layout.leftSpaceToView(view_indicateLine.superview, (btn.tag-100)*view_indicateLine.width);
        [view_indicateLine updateLayout];
}
-(void)showInView:(UIView *)view{
    [view addSubview:self];
    [self addSubview:_selectView];
    [UIView animateWithDuration:0.3 animations:^{
        _selectView.sd_layout.topSpaceToView(self, 0);
        [_selectView updateLayout];
        //inviteV.frame=CGRectMake(0,DEVH-inviteH, DEVW, inviteH);
    }];
}
-(void)removeSelectView{
    [UIView animateWithDuration:0.3 animations:^{
        //inviteV.frame=CGRectMake(0, DEVH, DEVW, inviteH);
        _selectView.sd_layout.topSpaceToView(self, -kSelectViewH);
        [_selectView updateLayout];
    } completion:^(BOOL finished) {
        [_selectView removeFromSuperview];
        [self removeFromSuperview];
    }];
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view==self) {
        return YES;
    }else{
        return NO;
    }
}
@end
